Skip to content

Method: static {...}

1: package de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.generous_tit_for_tat;
2:
3:
4: import de.fhdw.gaming.core.domain.GameException;
5: import de.fhdw.gaming.ipspiel23.dilemma.domain.DilemmaAnswerType;
6: import de.fhdw.gaming.ipspiel23.dilemma.domain.IDilemmaPlayer;
7: import de.fhdw.gaming.ipspiel23.dilemma.domain.IDilemmaState;
8: import de.fhdw.gaming.ipspiel23.dilemma.moves.IDilemmaMove;
9: import de.fhdw.gaming.ipspiel23.dilemma.moves.IDilemmaMoveFactory;
10: import de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.DilemmaMemoryStrategy;
11: import de.fhdw.gaming.ipspiel23.dilemma.strategy.internals.DilemmaRoundData;
12: import de.fhdw.gaming.ipspiel23.memory.GameMemoryCapacity;
13: import de.fhdw.gaming.ipspiel23.memory.IGameMemory;
14: import de.fhdw.gaming.ipspiel23.memory.IGameMemoryCapacity;
15:
16: import java.util.Map;
17: import java.util.Optional;
18: import java.util.Random;
19:
20: /**
21: * Cooperates on the first round and after the opponent cooperated.
22: * After a defection, it cooperates with a probability p = min(1 - (T-R/R-S), (R-P/T-P))
23: */
24: public class DilemmaGenerousTitForTatStrategy extends DilemmaMemoryStrategy {
25:
26: /**
27: * Used to generate stream of pseudorandom numbers: number generator.
28: */
29: private static final Random RANDOM = new Random();
30:
31: /**
32: * Creates a new instance of {@link DilemmaGenerousTitForTatStrategy}.
33: *
34: * @param moveFactory the move factory to use
35: */
36: protected DilemmaGenerousTitForTatStrategy(final IDilemmaMoveFactory moveFactory) {
37: super(moveFactory);
38: }
39:
40: @Override
41: protected IGameMemoryCapacity requestedMemoryCapacity() {
42: return GameMemoryCapacity.of(1);
43: }
44:
45:
46: @Override
47: public Optional<IDilemmaMove> computeNextMove(final int gameId, final IDilemmaPlayer player,
48: final IDilemmaState state)
49: throws GameException, InterruptedException {
50: final IGameMemory<DilemmaRoundData> memory = getMemoryForPlayer(player, state);
51: final IDilemmaMoveFactory moveFactory = getMoveFactory();
52: if (memory.size() == 0) { // cooperate for first round
53: return Optional.of(moveFactory.createCooperateMove());
54: }
55:
56: // imitate opponents move every other round thereafter
57: final DilemmaRoundData previousRound = memory.getRound(0, true);
58: final IDilemmaMove otherPlayersAction = previousRound.forOpponentOf(player).move();
59: if (otherPlayersAction.equals(getMoveFactory().createCooperateMove())) {
60: return Optional.of(otherPlayersAction);
61: } else {
62: final Map<DilemmaAnswerType, Map<DilemmaAnswerType, Double>> outcomes = player.getPossibleOutcomes();
63: // Get payouts from outcomes map. Payouts are statically defined, such that we can access
64: // them here correctly and calculate the probability of cooperation.
65: final double varR = outcomes.get(DilemmaAnswerType.COOPERATE).get(DilemmaAnswerType.COOPERATE); // reward
66: final double varS = outcomes.get(DilemmaAnswerType.COOPERATE).get(DilemmaAnswerType.DEFECT); // sucker
67: final double varT = outcomes.get(DilemmaAnswerType.DEFECT).get(DilemmaAnswerType.COOPERATE); // temptation
68: final double varP = outcomes.get(DilemmaAnswerType.DEFECT).get(DilemmaAnswerType.DEFECT); // punishment
69:
70: final double num1 = 1 - ((varT - varR) / (varR - varS));
71: final double num2 = (varR - varP) / (varT - varP);
72: // probability with which to cooperate, if enemy has defected in the previous round
73: final double probabilityForCooperation = Math.min(num1, num2);
74:
75: return Optional.of(RANDOM.nextDouble() <= probabilityForCooperation
76: ? getMoveFactory().createCooperateMove()
77: : getMoveFactory().createDefectMove());
78: }
79: }
80: }